package org.liuhy.jbpm4.utils
{
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	
	import org.liuhy.line.AbstractLine;
	import org.liuhy.line.CLine;
	import org.liuhy.line.rect.RectLine;
	import org.liuhy.utils.MyStringUtils;
	
	public class JbpmRectLineUtils
	{
		public function JbpmRectLineUtils()
		{
		}
		public static function getJbpmCLinePosStrs(p_cLine:CLine):String{
			var _str:String = "";
			var _line:AbstractLine = p_cLine.getFromLine();
			while(null!=_line){
				if(null!=_line.nextLine){
					_str = _str + (p_cLine.x + _line.x1) +  "," + (p_cLine.y + _line.y1) + ";" ;
				}else{
					_str = _str + (p_cLine.x + _line.x1) +  "," + (p_cLine.y + _line.y1) + ";" ;
					_str = _str + (p_cLine.x + _line.x2) +  "," + (p_cLine.y + _line.y2) + ";" ;
				}
				_line=_line.nextLine;
			}
			if(null!=_str && ""!=_str){
				_str = _str.substr(0,_str.length-1);
				_str = _str + ":";
			}
			return _str ;	
		}
		public static function convert2LineArray(p_strs:String,p_cLine:CLine):ArrayCollection{
			var _splitArray:Array = MyStringUtils.getInstance().split(p_strs,":");
			var _array:Array = MyStringUtils.getInstance().split(_splitArray[0],";");

			var _pointArray:ArrayCollection = new ArrayCollection();
			for(var i:int=0;i<_array.length;i++){
				var _strs:String = _array[i] as String;
				if(null!=_strs && ""!=_strs){
					var _posArray:Array = MyStringUtils.getInstance().split(_strs,",");
					if(_posArray.length>1){
						var _point:Point = new Point(int(_posArray[0]),int(_posArray[1]));
						_pointArray.addItem(_point);
					}
				}				
			}
			var _lineArray:ArrayCollection = new ArrayCollection();
			for(var i:int=0;i<_pointArray.length;i++){
				if(i<_pointArray.length-1){
					var _line:AbstractLine = new RectLine(_pointArray.getItemAt(i).x,_pointArray.getItemAt(i).y,_pointArray.getItemAt(i+1).x,_pointArray.getItemAt(i+1).y,p_cLine);
					_lineArray.addItem(_line);
				}				
			}
			for(var i:int=0;i<_lineArray.length;i++){
				if(i<_lineArray.length-1){
					(_lineArray.getItemAt(i) as  AbstractLine).nextLine  = (_lineArray.getItemAt(i+1) as  AbstractLine);
					(_lineArray.getItemAt(i+1) as  AbstractLine).preLine = (_lineArray.getItemAt(i) as  AbstractLine);
				}								
			}
			for(var i:int=0;i<_lineArray.length;i++){
				(_lineArray.getItemAt(i) as  AbstractLine).draw();					
			}
			return _lineArray;
		}
	}
}